package com.billdimmick.merkabah;

import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A base SQS exception.
 * @author Bill Dimmick <me@billdimmick.com>
 *
 */
public class SQSException extends Exception {
	private static final Log log=LogFactory.getLog(SQSException.class);
	private static final long serialVersionUID = 591079929540785483L;

	private final String requestId;
	private final String exceptionCode;
	
	/**
	 * Constructs a new exception.
	 * @param code the exception code - may be null or empty
	 * @param message the error message - may be null or empty
	 * @param requestId the request id - may be null or empty
	 */
	public SQSException(final String code, final String message, final String requestId) {
		super(message);
		Validate.notNull(code, "Provided exception code cannot be null.");
		this.exceptionCode=code;
		this.requestId=requestId;
	}
	
	/**
	 * Constructs a new exception from an existing exception.  This constructor is used when Merkabah
	 * has encountered an exception that does not originate from SQS and wants to communicate it to callers.
	 * SQSExceptions constructed this way always have no requestId and an exception code of <code>MerkabahException</code>
	 * @param e the exception to use
	 */
	public SQSException(final Exception e) {
		super(e);
		this.exceptionCode="MerkabahException";
		this.requestId=null;
	}

	/**
	 * Obtains the request id.
	 * @return the request id - may be null or empty
	 */
	public String getRequestId() {
		return requestId;
	}

	/**
	 * Obtains the exception code.
	 * @return the code - never null
	 */
	public String getExceptionCode() {
		return exceptionCode;
	}
}
